package actions;

import java.util.ArrayList;
import java.util.Map;

import javax.ejb.EJB;

import org.apache.struts2.ServletActionContext;

import beans.SearchBean;
import beans.SearchManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entities.Item;

public class LoadItemAdminAction extends ActionSupport {

	private Item item = new Item();

	@EJB
	private SearchManager searchMgr = new SearchBean();
	private ArrayList<String> itemTypes = new ArrayList<String>();

	public String execute() {

		int masterId = 0;
		itemTypes.add(Item.BOOK);
		itemTypes.add(item.CD);
		itemTypes.add(item.DVD);

		Map session = ActionContext.getContext().getSession();
		Integer id = (Integer) session.get("userId");

		if (id == null)
			return "loginerror";

		if (ServletActionContext.getRequest().getParameter("masterId") != null) {
			String masterIdStr = ServletActionContext.getRequest()
					.getParameter("masterId");
			masterIdStr = masterIdStr.trim();
			masterId = Integer.parseInt(masterIdStr);
			item = searchMgr.getItem(masterId);
			return "success";
		} else {
			return "error";
		}
	}

	public void setItem(Item item) {
		this.item = item;
	}

	public Item getItem() {
		return item;
	}

	public ArrayList<String> getItemTypes() {
		return itemTypes;
	}

	public void setItemTypes(ArrayList<String> itemTypes) {
		this.itemTypes = itemTypes;
	}

}
